package snake;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class SnakeGame {

	//窗口
	private JFrame frame;
	
	//战绩文字
	private JLabel lab;
	
	// 白色的游戏场
	private MyCanvas canvas;
	
	private int gwidth = 400;
	private int gheight = 400;
	
	private int width = 20;
	private int height = 20;
	
	private int xcount;
	private int ycount;
	
	private int eatCount;
	
	private long starTime;
	
	
	int direction = KeyEvent.VK_RIGHT;
	long  speed  = 400L;
	
	// 蛇
	private List<Rectangle> snake;
	
	// 食物
	private Rectangle food;
	
	//初始显示的窗口
	private void initFrame() {
		frame = new JFrame("贪吃蛇");
		frame.setSize(100, 100);
		frame.setLocation(500, 100);
		//随窗口关闭而关闭程序
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		
		//文本放到frame
		lab = new JLabel("战绩：");
		frame.add(lab, BorderLayout.NORTH);
		
		canvas = new MyCanvas();
		canvas.setSize(gwidth, gheight);
		canvas.setBackground(Color.WHITE);
		frame.add(canvas, BorderLayout.CENTER);
			
		frame.pack();
		//显示出来
		frame.setVisible(true);
		
		frame.addKeyListener(new MyKeyListener(this));
	}
	
	private void initData() {
		
		snake = new LinkedList<>();
		//初始化3个方块
		snake.add(new Rectangle(width * 3, height * 2, width, height));
		snake.add(new Rectangle(width * 2, height * 2, width, height));
		snake.add(new Rectangle(width * 1, height * 2, width, height));
		
		canvas.setSnake(snake);
		
		//生成食物
		food = new Rectangle(20, 20, width, height);
		this.generateFood();
		canvas.setRect(food);
		
		this.eatCount = 0;
		this.starTime = System.currentTimeMillis();
	}
	
	private Random random = new Random();
	
	private void generateFood() {
		
		outer:
		while(true) {
			//食物不能在蛇身上
			this.food.x = random.nextInt(xcount) * width;
			this.food.y = random.nextInt(ycount) * height;
			
			for(Rectangle rect : snake) {
				if(this.food.x == rect.x && this.food.y == rect.y) {
					continue outer;
				}
			}
			
			break;
		}
		
	}
	
	public void move() {
		
		while(true) {
			try {
				Thread.sleep(speed);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			Rectangle head = snake.get(0);
			Rectangle newHead = new Rectangle(head.x, head.y, width, height);
			
			switch(this.direction) {
			case KeyEvent.VK_RIGHT:
				newHead.x += width;
				if(newHead.x >= gwidth) {
					newHead.x = 0;
				}
				break;
			case KeyEvent.VK_LEFT:
				newHead.x -= width;
				if(newHead.x < 0) {
					newHead.x = gwidth - width;
				}
				break;	

			case KeyEvent.VK_DOWN:
				newHead.y += height;
				if(newHead.y >= gheight) {
					newHead.y = 0;
				}
				break;
			case KeyEvent.VK_UP:
				newHead.y -= height;
				if(newHead.y < 0) {
					newHead.y = gheight - height;
				}
				break;
			}
			
			//蛇运动
			snake.add(0,newHead);
			
			//吃食,不去尾
			if(newHead.x == food.x && newHead.y == food.y) {
				this.generateFood();
				this.eatCount++;
			}
			else {
				snake.remove(snake.size() - 1);
			}
			
			canvas.repaint();
			this.lab.setText("战绩：" + this.eatCount + " " + this.computeTime());
		}
	}
	
	private String computeTime() {
		long useTime = (System.currentTimeMillis() - this.starTime) / 1000;
		return "用时：" + (useTime / 60 / 60) + "小时" 
				+ (useTime % (60 * 60)) / 60 + "分"
				+ (useTime % 60) + "秒";
	}
	
	
	public SnakeGame(int gwidth, int gheight, int width, int height) {
		super();
		this.gwidth = gwidth;
		this.gheight = gheight;
		this.width = width;
		this.height = height;
		
		this.xcount = this.gwidth / this.width;
		this.ycount = this.gheight / this.height;

		initFrame();
		initData();
	}

	public static void main(String[] args) {
		new SnakeGame(400,400,20,20).move();
	}
	
}
